﻿Public Class LapPhieuChiForm
    Dim dt As DataTable
    Dim dt_chiphi As DataTable
    Dim dt_nganhang As DataTable
    Dim MaPhieuChi As Integer
    Dim MaUserChi As Integer
    Sub New(Optional ByVal MaPhieuChi As Integer = 0)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.        
        Me.MaPhieuChi = MaPhieuChi
        LoadChiPhi()
        LoadTaiKhoanNganHang()
        If MaPhieuChi <> 0 Then
            dt = QuanLyChiBUS.GetAllByMa(MaPhieuChi)
            If dt.Rows.Count > 0 Then
                txtMaPhieuChi.Text = MaPhieuChi
                txtChiTiet.Text = dt.Rows(0)("ChiTiet")
                txtSoTien.Text = dt.Rows(0)("SoTien")
                txtMaUserChi.Text = dt.Rows(0)("fullname")
                MaUserChi = dt.Rows(0)("MaUserChi")
                dtNgay.Text = dt.Rows(0)("Ngay")
                cbMaNganHang.SelectedValue = dt.Rows(0)("MaNganHang")
                cbMaChiPhi.SelectedValue = dt.Rows(0)("MaChiPhi")
            End If
        End If
    End Sub
    Private Sub LoadChiPhi()
        dt_chiphi = ChiPhiDAO.GetAll()
        cbMaChiPhi.DataSource = dt_chiphi
        cbMaChiPhi.DisplayMember = "Ten"
        cbMaChiPhi.ValueMember = "MaChiPhi"
    End Sub
    Private Sub LoadTaiKhoanNganHang()
        dt_nganhang = TaiKhoanNganHangDAO.GetAll(MainForm.current_user.MaUser)
        cbMaNganHang.DataSource = dt_nganhang
        cbMaNganHang.DisplayMember = "Ten"
        cbMaNganHang.ValueMember = "MaNganHang"
    End Sub
    Private Sub btSave_Click(sender As System.Object, e As System.EventArgs) Handles btSave.Click
        If txtSoTien.Text.Trim().Length = 0 Or Not IsNumeric(txtSoTien.Text) Then
            MessageBox.Show("Xin vui lòng nhập vào số tiền")
            Return
        End If
        If txtChiTiet.Text.Trim().Length = 0 Then
            MessageBox.Show("Xin vui lòng nhập vào chi tiết")
            Return
        End If
        If txtMaUserChi.Text.Trim().Length = 0 Then
            MessageBox.Show("Xin vui lòng chọn người chi")
            Return
        End If
        If cbMaNganHang.SelectedIndex < 0 Then
            MessageBox.Show("Xin vui lòng chọn tài khoản")
            Return
        End If
        If cbMaChiPhi.SelectedIndex < 0 Then
            MessageBox.Show("Xin vui lòng chọn loại chi phí")
            Return
        End If
        Dim obj As New PhieuChi
        With obj
            .Ngay = dtNgay.Text
            .MaNganHang = Integer.Parse(cbMaNganHang.SelectedValue)
            .MaUserChi = MaUserChi
            .MaUserLap = MainForm.current_user.MaUser
            .SoTien = Double.Parse(txtSoTien.Text)
            .MaChiPhi = Integer.Parse(cbMaChiPhi.SelectedValue)
            .ChiTiet = txtChiTiet.Text
        End With
        If QuanLyChiBUS.Write(obj, MaPhieuChi) Then
            MessageBox.Show("Lưu thành công")
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

    Private Sub btTim_Click(sender As System.Object, e As System.EventArgs) Handles btTim.Click
        Dim frm As New SearchUsersForm
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtMaUserChi.Text = frm.selected_user.Fullname
            MaUserChi = frm.selected_user.MaUser
        End If
    End Sub

    Private Sub btExit_Click(sender As System.Object, e As System.EventArgs) Handles btExit.Click
        Me.Dispose()
    End Sub

    Private Sub btnAddThuNhap_Click(sender As System.Object, e As System.EventArgs) Handles btnAddThuNhap.Click
        Dim frm As New ThuNhapForm
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            LoadChiPhi()
        End If
    End Sub

    Private Sub btnAddTaiKhoan_Click(sender As System.Object, e As System.EventArgs) Handles btnAddTaiKhoan.Click
        Dim frm As New TaiKhoanNganHangForm
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            LoadTaiKhoanNganHang()
        End If
    End Sub
End Class